home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
c't freeware shareware 1999 February
/
CT_SW9902.ISO
/
mac
/
software
/
wissen
/
daten
/
gnuplot.hqx
/
gnuplot.2.0b4
/
Interapplication
/
C Examples
/
gnuplotInterface.c
< prev
next >
Wrap
Text File
|
1997-04-27
|
12KB
|
350 lines
/* Version 2.0, DCS, 14/97 */
#include <AppleEvents.h>
#include <AERegistry.h>
#include <AEObjects.h>
#include <AEPackObject.h>
#include <Types.h>
#include <Files.h>
#include <Processes.h>
#include <string.h>
#include "gnuplotInterface.h"
static Boolean SearchForFile(OSType theCreatorType, OSType theFileType, FSSpec *theFile, short count);
static void ExtractReply(AEDesc *reply, char *result, long resultLen);
const OSType gnuplotID = 'GPSE';
/* Public functions the client application can use */
OSErr GnuplotLaunchApplication()
{
return (GnuplotLaunchApplicationCore(launchDontSwitch));
}
OSErr GnuplotLaunchApplicationToFront()
{
return (GnuplotLaunchApplicationCore(0));
}
OSErr GnuplotLaunchApplicationCore(long flags)
{
/* This routine is taken in part from Nick Triantos' "SafeLauncher" program */
LaunchParamBlockRec myLaunchParams ;
ProcessSerialNumber launchedProcessSN;
ProcessInfoRec processInfo;
OSErr launchErr ;
FSSpec sfFile;
Str255 name;
Boolean gnuplotRunning = false;
/* See if gnuplot is already running */
processInfo.processInfoLength = sizeof(processInfo);
processInfo.processName = (StringPtr) name;
processInfo.processAppSpec = &sfFile;
launchedProcessSN.highLongOfPSN = 0;
launchedProcessSN.lowLongOfPSN = kNoProcess;
while (GetNextProcess(&launchedProcessSN) == noErr) {
if (GetProcessInformation(&launchedProcessSN, &processInfo) == noErr) {
if (processInfo.processType == 'APPL' && processInfo.processSignature == 'GPSE') {
gnuplotRunning = true;
if (flags == 0)
SetFrontProcess(&(processInfo.processNumber));
return 0;
}
}
}
if (gnuplotRunning == false)
if (!SearchForFile('GPSE', 'APPL', &sfFile, 1))
return(-43);
myLaunchParams.launchAppSpec = &sfFile ;
myLaunchParams.launchBlockID = extendedBlock ;
myLaunchParams.launchEPBLength = extendedBlockLen ;
myLaunchParams.launchFileFlags = 0 ;
myLaunchParams.launchControlFlags = launchContinue + launchNoFileFlags +flags;
myLaunchParams.launchAppParameters = nil ;
launchErr = LaunchApplication(&myLaunchParams) ;
return(launchErr);
}
OSErr GnuplotExecuteCommand(char *theCommand, long length, char *response, long responseLength)
{
AppleEvent theEventToSend = {typeNull, NULL};
AEDesc theReply = {typeNull, NULL};
AEAddressDesc gnuplotAppDesc = {typeNull, NULL};
OSErr err = 0;
/* Create the gnuplot descriptor */
err = AECreateDesc(typeApplSignature, &gnuplotID, sizeof(gnuplotID), &gnuplotAppDesc);
/* Create the Apple Event */
if (err == noErr)
err = AECreateAppleEvent('GPSE', 'exec', &gnuplotAppDesc, -1, kAnyTransactionID, &theEventToSend);
if (err == noErr)
err = AEPutParamPtr(&theEventToSend, keyDirectObject, typeChar, theCommand, length);
if (err == noErr)
/* Send the Apple Event */
if (err == noErr)
err = AESend(&theEventToSend, &theReply, kAEWaitReply, kAENormalPriority, kAEDefaultTimeout, NULL, NULL);
ExtractReply(&theReply, response, responseLength);
/* Clean up after ourselves */
AEDisposeDesc(&gnuplotAppDesc);
AEDisposeDesc(&theEventToSend);
AEDisposeDesc(&theReply);
return err;
}
OSErr GnuplotDoScript(FSSpec *inputFile, char *response, long responseLength)
{
AppleEvent theEventToSend = {typeNull, NULL};
AEDesc theReply = {typeNull, NULL};
AEAddressDesc gnuplotAppDesc = {typeNull, NULL};
OSErr err;
/* Create the gnuplot descriptor */
err = AECreateDesc(typeApplSignature, &gnuplotID, sizeof(gnuplotID), &gnuplotAppDesc);
if (err == noErr)
/* Create the Apple Event */
if (err == noErr)
err = AECreateAppleEvent(kAEMiscStandards, 'dosc', &gnuplotAppDesc, -1, kAnyTransactionID, &theEventToSend);
if (err == noErr)
err = AEPutParamPtr(&theEventToSend, keyDirectObject, typeFSS, inputFile, sizeof(FSSpec));
if (err == noErr)
/* Send the Apple Event */
err = AESend(&theEventToSend, &theReply, kAEWaitReply, kAENormalPriority, kAEDefaultTimeout, NULL, NULL);
ExtractReply(&theReply, response, responseLength);
/* Clean up after ourselves */
AEDisposeDesc(&gnuplotAppDesc);
AEDisposeDesc(&theEventToSend);
AEDisposeDesc(&theReply);
return err;
}
OSErr GnuplotPlot(FSSpec *inputFile, long fileCount, char *data, long dataSize,
short useClipboard, short do3d, long lineType, char *response, long responseLength)
{
AppleEvent theEventToSend = {typeNull, NULL};
AEDesc appDesc = {typeNull, NULL}, theReply = {typeNull, NULL};
AEDesc theObject = {typeNull, NULL}, clipDesc = {typeNull, NULL};
AEAddressDesc gnuplotAppDesc = {typeNull, NULL};
AEDescList plotData = {typeNull, NULL};
OSErr err, i;
long plotType = 'plot';
DescType clipboardType = 'clip';
GnuplotInitializeObjects();
/* Create the gnuplot descriptor */
err = AECreateDesc(typeApplSignature, &gnuplotID, sizeof(gnuplotID), &gnuplotAppDesc);
if (do3d)
plotType = 'splt';
/* Create the Apple Event */
if (err == noErr)
err = AECreateAppleEvent('GPLT', plotType, &gnuplotAppDesc, -1, kAnyTransactionID, &theEventToSend);
if (err == noErr)
err = AECreateList(NULL, 0, 0, &plotData);
if (inputFile && (err == noErr)) {
for (i = 0; i < fileCount && err == noErr; i++) {
err = AEPutPtr(&plotData, 0, typeFSS, &inputFile[i], sizeof(FSSpec));
}
}
if (dataSize && data != NULL && (err == noErr)) {
err = AEPutPtr(&plotData, 0, typeChar, data, dataSize);
}
if (useClipboard && (err == noErr)) {
/* Now create the object specifer */
/* Create a descriptor for the application */
AECreateDesc(typeNull, NULL, 0, &appDesc);
err = AECreateDesc(typeType, &clipboardType, sizeof(DescType), &clipDesc);
err = CreateObjSpecifier(cProperty, &appDesc, formPropertyID, &clipDesc, 0, &theObject);
err = AEPutDesc(&plotData, 0, &theObject);
}
if (err == noErr)
err = AEPutParamDesc(&theEventToSend, keyDirectObject, &plotData);
if (lineType != gp_type_default && err == noErr) {
err = AEPutParamPtr(&theEventToSend, 'line', typeEnumerated, &lineType, sizeof(lineType));
}
if (err == noErr)
/* Send the Apple Event */
err = AESend(&theEventToSend, &theReply, kAEWaitReply, kAENormalPriority, kAEDefaultTimeout, NULL, NULL);
ExtractReply(&theReply, response, responseLength);
/* Clean up after ourselves */
AEDisposeDesc(&theObject);
AEDisposeDesc(&clipDesc);
AEDisposeDesc(&appDesc);
AEDisposeDesc(&plotData);
AEDisposeDesc(&theEventToSend);
AEDisposeDesc(&gnuplotAppDesc);
AEDisposeDesc(&theReply);
return err;
}
void GnuplotQuitApplication()
{
AppleEvent theEventToSend = {typeNull, NULL};
AEDesc theReply = {typeNull, NULL};
AEAddressDesc gnuplotAppDesc = {typeNull, NULL};
OSErr err;
/* Create the gnuplot descriptor */
err = AECreateDesc(typeApplSignature, &gnuplotID, sizeof(gnuplotID), &gnuplotAppDesc);
/* Create the Apple Event */
if (err == noErr)
err = AECreateAppleEvent(typeAppleEvent, kAEQuitApplication, &gnuplotAppDesc, -1, kAnyTransactionID, &theEventToSend);
/* Send the Apple Event */
if (err == noErr)
err = AESend(&theEventToSend, &theReply, kAENoReply, kAENormalPriority, kNoTimeOut, NULL, NULL);
/* Clean up after ourselves */
AEDisposeDesc(&gnuplotAppDesc);
AEDisposeDesc(&theEventToSend);
AEDisposeDesc(&theReply);
}
/* Call GnuplotInitializeObjects before doing anything that tries to get data from gnuplot */
/* Call GnuplotShutDownObjects to reclaim any memory from the object stuff */
typedef enum { notInitialized, initializedOnce, initializedNow } Init;
static AEDesc basePictureObjDesc = {typeNull, NULL};
static Init initialized = notInitialized;
OSErr GnuplotInitializeObjects()
{
OSErr err = 0;
if (initialized == notInitialized)
err = AEObjectInit();
initialized = initializedOnce;
return err;
}
/* Call GnuplotShutDownObjects to reclaim any memory */
void GnuplotShutDownObjects()
{
}
OSErr GnuplotSetWorkingFolder(char *newFolder)
{
AEDesc appDesc, pathDesc, theObject, theReply, gnuplotAppDesc;
AEDesc pathTypeDesc;
AppleEvent theEventToSend;
DescType folderType = 'wfdr';
int length;
OSErr err;
if (initialized != initializedNow)
GnuplotInitializeObjects();
length = strlen(newFolder);
/* Create a descriptor for the application */
AECreateDesc(typeNull, NULL, 0, &appDesc);
/* Create a descriptor for the new path */
AECreateDesc(typeChar, newFolder, length, &pathDesc);
/* Now create the object specifer */
err = AECreateDesc(typeType, &folderType, sizeof(DescType), &pathTypeDesc);
CreateObjSpecifier(cProperty, &appDesc, formPropertyID, &pathTypeDesc, 0, &theObject);
/* Create the gnuplot descriptor */
err = AECreateDesc(typeApplSignature, &gnuplotID, sizeof(gnuplotID), &gnuplotAppDesc);
/* Create the Apple Event */
err = AECreateAppleEvent(kAECoreSuite, kAESetData, &gnuplotAppDesc, -1, kAnyTransactionID, &theEventToSend);
err = AEPutParamDesc(&theEventToSend, keyDirectObject, &theObject);
err = AEPutParamDesc(&theEventToSend, keyAEData, &pathDesc);
err = AESend(&theEventToSend, &theReply, kAENoReply + kAENeverInteract, kAENormalPriority,
kAEDefaultTimeout, NULL, NULL);
err = AEDisposeDesc(&theEventToSend);
err = AEDisposeDesc(&gnuplotAppDesc);
err = AEDisposeDesc(&theObject);
err = AEDisposeDesc(&pathTypeDesc);
err = AEDisposeDesc(&pathDesc);
err = AEDisposeDesc(&appDesc);
return(err);
}
/* Private functions and data the client app shouldn't use */
void ExtractReply(AEDesc *reply, char *result, long resultLen)
{
OSErr err;
DescType iWant = typeChar;
DescType iGot;
long length;
if (result == NULL || resultLen == 0)
return;
err = AEGetParamPtr(reply, keyDirectObject, iWant, &iGot, result, resultLen-1, &length);
if (err != noErr)
result[0] = 0;
else
result[length] = 0;
}
static Boolean SearchForFile(OSType theCreatorType, OSType theFileType, FSSpec *theFile, short count)
{
HParamBlockRec myHPBRec;
OSErr myError;
CInfoPBRec spec1, spec2;
char *buffer;
long bufferSize = 16384;
FSSpec theApp;
ProcessInfoRec appInformation;
ProcessSerialNumber serialNumber;
serialNumber.lowLongOfPSN = kCurrentProcess;
serialNumber.highLongOfPSN = 0;
appInformation.processInfoLength = sizeof(ProcessInfoRec);
appInformation.processName = NULL;
appInformation.processAppSpec = &theApp;
GetProcessInformation(&serialNumber, &appInformation);
buffer = NewPtr(bufferSize);
if (buffer == NULL)
return(false);
myHPBRec.csParam.ioCompletion = NULL;
myHPBRec.csParam.ioNamePtr = NULL;
myHPBRec.csParam.ioVRefNum = theApp.vRefNum;
myHPBRec.csParam.ioMatchPtr = theFile;
myHPBRec.csParam.ioReqMatchCount = count;
myHPBRec.csParam.ioSearchBits = fsSBFlFndrInfo;
myHPBRec.csParam.ioSearchInfo1 = &spec1;
myHPBRec.csParam.ioSearchInfo2 = &spec2;
myHPBRec.csParam.ioSearchTime = 0;
myHPBRec.csParam.ioCatPosition.initialize = 0;
myHPBRec.csParam.ioOptBuffer = buffer;
myHPBRec.csParam.ioOptBufSize = bufferSize;
spec1.hFileInfo.ioNamePtr = NULL;
spec1.hFileInfo.ioFlFndrInfo.fdType = theFileType;
spec1.hFileInfo.ioFlFndrInfo.fdCreator = theCreatorType;
spec1.hFileInfo.ioFlAttrib = 0;
spec1.hFileInfo.ioFlParID = 0;
spec2 = spec1;
spec2.hFileInfo.ioFlAttrib = 0x10;
spec2.hFileInfo.ioFlFndrInfo.fdCreator = theCreatorType;
spec2.hFileInfo.ioFlFndrInfo.fdType = -1;
spec2.hFileInfo.ioFlFndrInfo.fdFlags = 0;
spec2.hFileInfo.ioFlFndrInfo.fdLocation.h = spec2.hFileInfo.ioFlFndrInfo.fdLocation.v = 0;
spec2.hFileInfo.ioFlFndrInfo.fdFldr = 0;
spec2.hFileInfo.ioFlParID = 0;
myError = PBCatSearchSync((CSParamPtr)&myHPBRec);
DisposePtr(buffer);
if (myHPBRec.csParam.ioReqMatchCount == myHPBRec.csParam.ioActMatchCount)
myError = eofErr;
if (myHPBRec.csParam.ioActMatchCount == 0)
myError = 0;
if (myError == eofErr)
return(true);
else
return(false);
}